<?php
/**新健宝测评8in1
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/4
 * Time: 17:07
 */

namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\IdEncryptService;
use CategoryRepository;
use TestingConfig;
use TestingDetailService;
use UserRecordService;
use ResultTypeService;
use ResultAttributeService;
use TestingItemService;
use TestingDeveloperService;
use TestingXjbHelper;
use XjbUserRecordService;
use XjbCancerItemService;
use CancerXjbHelper;
use TestingXjbAlgoHelper;
class CancerController extends Controller
{
    //返回8in1题目
    public function testingitem(){
        $dev_code = TestingConfig::getValue('parter.xjb.display_dev_code');
        $dataCode = array('union_code'=>$dev_code,'enable'=>1);
        $unionCode = TestingDeveloperService::checkUnionCode($dataCode);
        $tid =  TestingConfig::getValue('parter.xjb.cancer.all.tid');
        if($unionCode){
            $result = XjbCancerItemService::getItem($tid);
            if($result){
                return apiSuccess($result);
            }else{
                return apiError();
            }
        }else{
            return apiError();
        }

    }

    /**提交接口
     * @param Request $request
     * @return array
     */
    public function submit(Request $request){
        $answers = $request->input('answers');
        $uid = $request->input('uid');
        $extra =  $request->input('extra');
        if($answers){
            $answersArr = CancerXjbHelper::replaceFormat($answers);
            foreach($answersArr as $k => $v){
                $tid = $k;
                $answerstr = CancerXjbHelper::arrToStr($v);
                $list[$k] = $this->post_submit($tid,$uid,$answerstr,$extra);
            }
            if(count($list)==8){
                return apiSuccess();
            }else{
                return apiError();
            }
        }else{
            return apiError();
        }
    }

    /*存储单项测评数据
     * @param $tid
     * @param $uid
     * @param $answer
     * @param $extra
     * @return mixed
     */
    public function post_submit($tid,$uid,$answer,$extra){
        $data['answers']=$answer;
        $data['uid'] = $uid;
        $data['tid'] = $tid;
        $data['extra'] = $extra;
        $typeFourExpress = TestingItemService::typeFourItem($tid);
        $typeFourExpress = empty($typeFourExpress) ? null : $typeFourExpress;
        $reslut = TestingXjbAlgoHelper::calculate($tid,$answer,$typeFourExpress);
        $data['final_answers']=$reslut['finalAnswerStr'];
        $Probability=$this -> testingProbability($tid,$reslut['totalScore'],$reslut['finalAnswerStr']);
        $data['score']=$Probability['score']*100;
        $data['conclusion'] =$Probability['title'];
        $reslut = XjbUserRecordService::stroeResult($data);
        return $reslut;
    }

    /**返回测评概率值
     * @param $tid
     * @param $score
     * @param $answerstr
     * @return  null
     *
     */
    public function testingProbability($tid,$score,$answerstr){
        $detail = TestingDetailService::getById($tid);
        if($detail) {
            $result_type_id_arr = explode(',', $detail['result_type']);
            $result = ResultTypeService::result($tid,$result_type_id_arr,$score);
            $answers =TestingXjbHelper::strToArr($answerstr);
            foreach($answers as $k => $v){
                if($tid == 4 && $v['weight'] == 0 && $v['index'] == 1){
                    $result['title']="无风险";
                }
            }
            $result['score']=TestingXjbHelper::conclusion($tid,$result['factor_str'],$answers,$score);
            unset($result['factor_str']);
            return $result;
        }else{
            return null;
        }
    }

    /**根据用户id获取8项测评结论部分
     * @param $uid
     * @return array
     */
    public function testingAll($uid){
        $result=[];
        for($i=4;$i<=11;$i++){
            $result[$i] = XjbUserRecordService::getByUid($uid,$i);
        }
        foreach($result as $k => $v){
            $data[$k]=$this->testingResult($v,$k);
        }
        $res = CancerXjbHelper:: formatResult($data);
        if($result){
            return apiSuccess($res);
        }else{
            $ex['extra']=null;
            $ex['list']=null;
            return apiSuccess($ex);
        }

    }


    /**获取结论首页单项结论
     * @param $result
     * @param $tid
     * @return array
     */
    public function testingResult($result,$tid){
        if($result){
            $data['tid']=IdEncryptService::encryption($tid,'encode');
            $data['score']=$result['score'];
            $data['avg_score']=TestingXjbHelper::avgScore($tid,$result['answers']);
            $data['result']=$result['conclusion'];
            $data['description']=$this->testingDescription($tid);
            $data['extra'] = $result['extra'];
            return $data;
        }else{
            return apiError();
        }
    }

    /**获取癌症描述
     * @param $tid
     */
    public function testingDescription($tid){
        $result = TestingDetailService::getById($tid);
        return $result['gist'];
    }


    /**单个测评结论
     * @param $tidEncode
     * @param $uid
     * @return array
     */
    public function testingSingle($tidEncode,$uid){
        $tidArr = IdEncryptService::encryption($tidEncode,'decode');
        $tid = $tidArr[0];
        $data = [];
        $result = XjbUserRecordService::getByUid($uid,$tid);
        if($result){
            $data['score']=$result['score'];
            $data['avg_score']=TestingXjbHelper::avgScore($tid,$result['answers']);
            $data['result']=$result['conclusion'];
            $data['description']=$this->testingDescription($tid);
            $data['extra'] = $result['extra'];
            $answerArr = TestingXjbHelper::strToArr($result['answers']);
            $data['factor']= TestingXjbHelper::getSingleFactor($answerArr,$tid,$result['final_answers']);
            return apiSuccess($data);
        }else{
            return apiError();
        }
    }




}